<template>
	<view class="content">
		<view>
			曝光人姓名
			<text class="huang">※</text>
		</view>
		<input type="text" v-model="upuser" placeholder="请输入姓名" />
		<view>
			曝光人手机号
			<text class="huang">※</text>
		</view>
		<input type="number" v-model="phone" placeholder="请输入手机号" />
		<view>
			曝光描述
			<text class="huang">※</text>
		</view>
		<input type="text" v-model="content" placeholder="请输入描述" />
		<view>
			举报地址
			<text class="huang">※</text>
		</view>
		<!-- <picker id="city" @change="bindPickerChange" :value="cityIndex" mode="region">
			<view class="uni-input">{{ city[0] }}，{{ city[1] }}，{{ city[2] }}</view>
		</picker> -->
		<view class="" style="display: flex;">
			<!-- <view class="uni-input" style="color: #333333;">河北省</view> -->
			<picker mode="selector" id="SHI" :value="SHIindex" range-key="label" :range="SHI" @change="bindPickerChange">
				<view class="uni-input">{{ SHI[SHIindex]['label'] }}</view>
			</picker>
			<picker v-if="xianShow" mode="selector" id="XIAN" :value="XIANindex" range-key="label" :range="XIAN" @change="bindPickerChange">
				<view class="uni-input">{{ XIAN[XIANindex]['label'] }}</view>
			</picker>
		</view>
		<picker id="JD" @change="bindPickerChange" :value="JDindex" range-key="label" :range="JD" v-if="jdShow">
			<view class="uni-input">{{ JD[JDindex]['label'] }}</view>
		</picker>
		<picker id="PCS" @change="bindPickerChange" :value="PCSindex" range-key="label" :range="PCS" v-if="pcsShow">
			<view class="uni-input">{{ PCS[PCSindex]['label'] }}</view>
		</picker>
		<input type="text" v-model="address" style="height: 100upx;" placeholder="请输入详细地址" />

		<view>
			<text>照片:({{ imageList.length }}/4)</text>
			<text style="color: #fcd700;padding-left: 5upx;">至少上传1张</text>
			<view class="uni-uploader">
				<view class="uni-uploader-body">
					<view class="uni-uploader__files" style="width: 100%;display: flex;">
						<block v-if="imageList.length !== 0">
							<view v-for="val of imageList" :key="val.id" class="uni-uploader__file" style="position: relative;width: 30%;margin-right: 20upx;">
								<image class="uni-uploader__img" :src="val.src"></image>
								<view class="close-view" @tap="close(val.id)">x</view>
							</view>
						</block>
						<view class="uni-uploader__input-box" style="width: 30%;margin: 10upx;" v-if="imageList.length < 4">
							<view class="uni-uploader__input" @tap="chooseImg()"></view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<button type="primary" style="background-color:#fcd700;" @tap="chaKong">曝光</button>
	</view>
</template>

<script>
import CHONG from '../../static/js/function.js';
import { mapState, mapMutations } from 'vuex';
export default {
	computed: mapState(['URL']),
	data() {
		// 初始值
		return {
			upuser: '', //姓名
			phone: '', //手机号
			content: '', //描述
			
			SHICode: '',
			XIANCode: '',
			JDCode: '',
			PCSCode: '',
			
			address: '', //详细地址
			tupian: '',

			imageList: [],
			i: 0,
			arr: [],

			isShow: true,
			xianShow:false,
			pcsShow:false,
			jdShow:false,
			// cityIndex: [2, 0, 0],
			PCSindex: 0,
			JDindex: 0,
			SHIindex: 0,
			XIANindex: 1,

			// city: ['河北省', '石家庄市', '长安区'],
			// PCS: [{ value: '"130102004"', label: '光华路派出所' }],
			// JD: [{ value: '130102001', label: '胜利北街' }]
			SHI: [],
			XIAN: [],
			PCS: [],
			JD: [],
		};
	},
	methods: {
		bindPickerChange(e) {
			console.log(e);
// 			if (e.currentTarget.id === 'city') {
// 				this.city = e.target.value;
// 				this.cityCode = e.target.code[2];
// 
// 				this.getPaichusuo(e.target.code[2]);
// 				this.getJeidao(e.target.code[2]);
// 			}
			if (e.currentTarget.id === 'SHI') {
				this.SHIindex = e.target.value;
				this.SHICode = this.SHI[this.SHIindex]['value'];
				this.XIAN = [];
				this.XIANCode = "";
				this.XIANindex = 1;
				
				this.JD = [];
				this.JDCode = "";
				this.JDindex = 0;
				
				this.PCS = [];
				this.PCSCode = "";
				this.PCSindex = 0;
				
				
				this.getXian(this.SHICode);
			} else if (e.currentTarget.id === 'XIAN') {
				this.JD = [];
				this.JDCode = "";
				this.JDindex = 0;
				
				this.PCS = [];
				this.PCSCode = "";
				this.PCSindex = 0;
				
				this.XIANindex = e.target.value;
				this.XIANCode = this.XIAN[this.XIANindex]['value'];
				this.getPaichusuo(this.XIANCode);
				this.getJeidao(this.XIANCode);
			} else if (e.currentTarget.id === 'PCS') {
				this.PCSindex = e.target.value;
				this.PCSCode = this.PCS[this.PCSindex]['value'];
			} else if (e.currentTarget.id === 'JD') {
				this.JDindex = e.target.value;
				this.JDCode = this.JD[this.JDindex]['value'];
			} else if (e.currentTarget.id === 'CSdate') {
				this.dogAgeValue = this.CSdate = e.target.value;
			} else if (e.currentTarget.id === 'ZSdate') {
				this.lastDate = this.ZSdate = e.target.value;
			} else if (e.currentTarget.id === 'ZSD') {
				this.ZSDindex = e.target.value;
				this.ZSDCode = this.ZSD[this.ZSDindex]['value'];
			}
		},
		postJuBao() {
			let data = {
				title: this.address, //详细地址
				content: this.content, //描述信息
				code: this.JDCode || this.XIANCode, //三级联动
				pcode: this.PCSCode, //派出所
				upuser: this.upuser, //曝光人姓名
				phone: this.phone, //曝光人电话
				tupian: this.tupian //图片
			};
			CHONG.tip('提交中', 0);
			console.log(data);
			uni.request({
				url: this.URL + 'addexposed',
				data: data,
				success: res => {
					console.log(res.data);
					CHONG.tip(res.data.msg);
					setTimeout(() => {
						uni.navigateBack({});
					}, 1500);
				}
			});
		},
		chaKong() {
			if (!this.upuser) {
				CHONG.tip('姓名不能为空');
				return;
			} else if (!this.phone ) {
				CHONG.tip('手机号不能为空');
				return;
			}else if (this.phone.length !== 11 ) {
				CHONG.tip('手机号格式不正确');
				return;
			} else if (!this.content) {
				CHONG.tip('描述不能为空');
				return;
			} else if (!this.XIANCode) {
				CHONG.tip('请选择举报地址', 1);
				return;
			} else if (!this.PCSCode) {
				CHONG.tip('请选择派出所', 1);
				return;
			} else if (!this.address) {
				CHONG.tip('请填写详细地址', 1);
				return;
			} else if (!this.imageList.length) {
				CHONG.tip('请上传图片');
				return;
			}
			this.upImg();
		},
		upImg() {
			uni.uploadFile({
				url: this.URL + 'uploadreport',
				filePath: this.imageList[this.i]['src'],
				name: 'tupian',
				success: uploadFileRes => {
					let json = JSON.parse(uploadFileRes.data);
					this.arr.push(json.data[0].fileName);
					this.i++;
					console.log(json.data[0].fileName);
					console.log(this.i, this.imageList.length);
					if (this.i === this.imageList.length) {
						let tupian = this.arr.join(',');
						this.tupian = tupian;
						this.postJuBao();
					} else {
						this.upImg();
					}
				}
			});
		},
		chooseImg() {
			//选择图片
			uni.chooseImage({
				sourceType: ['camera', 'album'],
				count: 4,
				success: res => {
					this.imageList.push({
						src: res.tempFilePaths[0],
						id: res.tempFilePaths[0]
					});
				}
			});
		},
		close(e) {
			let arr = [];
			this.imageList.map((val, index) => {
				if (val.id !== e) {
					arr.push(val);
				}
			});
			this.imageList = arr;
		},
		getShi() {
			uni.request({
				url: this.URL + 'shi',
				success: res => {
					// console.log("shi");
					// console.log(res.data);
					if (res.data.success) {
						this.SHI = res.data.data;
						this.getXian(this.SHI[0]['value']);
						this.SHICode = this.SHI[0]['value'];
						this.xianShow = true;
						// console.log(this.SHICode);
					}
				}
			});
		},
		getXian(code) {
			uni.request({
				url: this.URL + 'xian',
				data: {
					code: code
				},
				success: res => {
					console.log("xian");
					console.log(res.data);
					if (res.data.success) {
						this.XIAN = res.data.data;
						this.XIANCode = this.XIAN[1]['value'];
						this.getPaichusuo(this.XIANCode);
						this.getJeidao(this.XIANCode);
					}
				}
			});
		},
		getPaichusuo(code) {
			uni.request({
				url: this.URL + 'police',
				data: {
					code: code
				},
				success: res => {
					if (res.data.success) {
						this.PCS = res.data.data;
						this.pcsShow = true;
						this.PCSCode = this.PCS[this.PCSindex]['value'];
					} else {
						CHONG.tip("该地区没有派出所，请重新选择");
						this.pcsShow = false;
						this.PCSCode = '';
					}
				}
			});
		},
		getJeidao(code) {
			uni.request({
				url: this.URL + 'jie',
				data: {
					code: code
				},
				success: res => {
					if (!res.data.success) {
						// CHONG.tip("该地区没有街道，请重新选择");
						this.jdShow = false;
						this.JDCode = '';
					} else {
						this.JD = res.data.data;
						this.jdShow = true;
						this.JDCode = this.JD[this.JDindex]['value'];
					}
				}
			});
		},
	},
	onLoad() {
		this.getShi();

		// this.getPaichusuo(this.cityCode);
		// this.getJeidao(this.cityCode);
	}
};
</script>

<style lang="scss">
.content {
	font-size: 26upx;
	padding: 20upx;
	.huang {
		color: $chong-iconbg-color;
	}
	input {
		color: $chong-text-color;
		font-size: 24upx;
		margin: 10upx 0;
		padding: 4upx 20upx;
		border: {
			radius: 10upx;
			style: solid;
			width: 1upx;
			color: rgba($chong-text-color, 0.2);
		}
		background: white;
	}
}
/* 上传 */
.uni-uploader {
	flex: 1;
	flex-direction: column;
	width: 100%;
}
.uni-uploader-info {
	color: #b2b2b2;
}
.uni-uploader-body {
	margin-top: 16upx;
}
.uni-uploader__files {
	display: flex;
	flex-direction: row;
	flex-wrap: wrap;
}
.uni-uploader__file {
	width: 208upx;
	width: 30%;
	height: 208upx;
	margin: 10px 0px;
}
.uni-uploader__img {
	display: block;
	width: 208upx;
	height: 208upx;
}
.uni-uploader__input-box {
	position: relative;
	margin: 10upx auto;
	width: 208upx;
	height: 208upx;
	border: 2upx solid #d9d9d9;
}
.uni-uploader__input-box:before,
.uni-uploader__input-box:after {
	content: ' ';
	position: absolute;
	top: 50%;
	left: 50%;
	-webkit-transform: translate(-50%, -50%);
	transform: translate(-50%, -50%);
	background-color: #d9d9d9;
}
.uni-uploader__input-box:before {
	width: 4upx;
	height: 79upx;
}
.uni-uploader__input-box:after {
	width: 79upx;
	height: 4upx;
}
.uni-uploader__input-box:active {
	border-color: #999999;
}
.uni-uploader__input-box:active:before,
.uni-uploader__input-box:active:after {
	background-color: #999999;
}
.uni-uploader__input {
	position: absolute;
	z-index: 1;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	opacity: 0;
}
.uni-uploader__file,
.uni-uploader__input-box {
	width: 50%;
}
.close-view {
	text-align: center;
	line-height: 14px;
	height: 16px;
	width: 16px;
	border-radius: 50%;
	background: #ff5053;
	color: #ffffff;
	position: absolute;
	top: -6px;
	right: -4px;
	font-size: 12px;
}
.uni-input {
	color: $chong-text-color;
	font-size: 24upx;
	margin: 10upx 0;
	padding: 17upx 20upx;

	border: {
		radius: 10upx;
		style: solid;
		width: 1upx;
		color: rgba($chong-text-color, 0.2);
	}

	background: white;
}
.fixed {
		position: fixed;
		bottom: 50upx;
		right: 50upx;
		width: 80upx;
		height: 80upx;

		border: {
			radius: 50%;
		}

		box-shadow: 4upx 4upx 9upx #333333;
		opacity: 0.8;
		background: $chong-iconbg-color url(../../static/img/icon001.png);
		background-size: 100% 100%;
	}
</style>
